/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CargaProfesiones.java
 *
 * Created on Mar 23, 2011, 10:24:20 PM
 */

package forms;

import java.awt.Cursor;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import managers.ManagerProfesiones;

/**
 *
 * @author nMoncho
 */
public class CargaProfesiones extends javax.swing.JFrame {

    /** Creates new form CargaProfesiones */
    public CargaProfesiones() {
        initComponents();
        this.setLocation(200, 200);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableProfesiones = new javax.swing.JTable();
        botonCerrar = new javax.swing.JButton();
        botonGuardar = new javax.swing.JButton();
        botonAgregar = new javax.swing.JButton();
        botonBorrar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Gestionar Profesiones");
        setIconImage(new ImageIcon(getClass().getResource("/resources/arrow-branch-bgr-16-ns.png")).getImage());

        jLabel1.setText("Listado de Profesiones:");

        tableProfesiones.setModel(gestor.getModel());
        tableProfesiones.setToolTipText("Listado de Profesiones");
        tableProfesiones.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tableProfesionesKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(tableProfesiones);

        botonCerrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-cross-16-ns.png"))); // NOI18N
        botonCerrar.setText("Cerrar");
        botonCerrar.setToolTipText("Cerrar Ventana");
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarActionPerformed(evt);
            }
        });

        botonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/badge-square-check-16-ns.png"))); // NOI18N
        botonGuardar.setText("Guardar");
        botonGuardar.setToolTipText("Guardar Profesiones");
        botonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonGuardarActionPerformed(evt);
            }
        });

        botonAgregar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/plus-16-ns.png"))); // NOI18N
        botonAgregar.setToolTipText("Agregar Profesion");
        botonAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonAgregarActionPerformed(evt);
            }
        });

        botonBorrar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/minus-16-ns.png"))); // NOI18N
        botonBorrar.setToolTipText("Borrar Profesion");
        botonBorrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonBorrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 255, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(botonAgregar, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(botonBorrar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(83, 83, 83)
                .addComponent(botonGuardar)
                .addGap(18, 18, 18)
                .addComponent(botonCerrar)
                .addContainerGap(25, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonCerrar, botonGuardar});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(botonAgregar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonBorrar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonGuardar))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {botonAgregar, botonBorrar});

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarActionPerformed
        this.dispose();
    }//GEN-LAST:event_botonCerrarActionPerformed

    private void botonBorrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonBorrarActionPerformed
        borrarFila();
    }//GEN-LAST:event_botonBorrarActionPerformed

    private void tableProfesionesKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableProfesionesKeyPressed
        if(evt.getKeyCode() == KeyEvent.VK_DELETE){
            borrarFila();
        }
    }//GEN-LAST:event_tableProfesionesKeyPressed

    private void botonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonGuardarActionPerformed
        int respuesta = JOptionPane.showConfirmDialog(this, "Los cambios se haran permanentes.\n¿Estas seguro que deseas guardar?", "Confirmacion", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_NO_OPTION);
        if(respuesta == JOptionPane.YES_OPTION){
            setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
            boolean exito = gestor.guardarCambios();
            setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            if(exito)
                JOptionPane.showMessageDialog(this, "Los cambios se guardaron exitosamente", "Exito", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
            else
                JOptionPane.showMessageDialog(this, "Ocurrio un error tratando de guardar los datos.\nPor favor, revisa lo datos y volve a intentarlo.", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);

        }
    }//GEN-LAST:event_botonGuardarActionPerformed

    private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonAgregarActionPerformed
        String nombre = JOptionPane.showInputDialog(this, "Ingresa el nombre de la Profesion.", "Ingreso", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_OPTION);
        if(nombre != null && nombre.trim().length() > 0){
            boolean exito = gestor.agregarProfesion(nombre.trim());
            if(!exito)
                JOptionPane.showMessageDialog(this, "El nombre de la Profesion que ingresaste ya existe", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
            else
                tableProfesiones.updateUI();
        }else
            JOptionPane.showMessageDialog(this, "Tenes que ingresar un nombre para la Profesion", "Error", JOptionPane.ERROR_MESSAGE+JOptionPane.YES_OPTION);
    }//GEN-LAST:event_botonAgregarActionPerformed

    private void borrarFila() {
        int respuesta = JOptionPane.showConfirmDialog(this, "¿Estas seguro que deseas borrar esta profesion?", "Confirmacion", JOptionPane.INFORMATION_MESSAGE+JOptionPane.YES_NO_OPTION);
        if(respuesta == JOptionPane.YES_OPTION){
            int[] selectedRows = this.tableProfesiones.getSelectedRows();
            int row;
            for(int i=selectedRows.length-1; i>-1;i--){
                row = selectedRows[i];
                gestor.borrarProfesion(row);
            }
        }

        tableProfesiones.updateUI();
    }
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new CargaProfesiones().setVisible(true);
            }
        });
    }

    private ManagerProfesiones gestor = new ManagerProfesiones();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonAgregar;
    private javax.swing.JButton botonBorrar;
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tableProfesiones;
    // End of variables declaration//GEN-END:variables

}
